<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-form :model="page" ref="page" label-width="80px">
          <el-row type="flex">
            <el-col>
              <el-form-item prop="tags" label="标签名称" size="small" style="width: 100%">
                <el-input placeholder="请输入" size="small" autocomplete="off" v-model="page.tags"/>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item prop="province" label="城市" size="small">
                <el-select size="small" v-model="page.province" @change="cityChange" style="width: 100%">
                  <el-option v-for="item in cityList" :key="item.value" :label="item.label" :value="item.label"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item prop="city" label="地区" size="small">
                <el-select size="small" v-model="page.city" style="width: 100%" >
                  <el-option v-for="item in areaList" :key="item.value" :label="item.label" :value="item.label" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item prop="shortName" label="企业简称" size="small" style="width: 100%" >
                <el-input size="small" v-model="page.shortName" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex">
            <el-col :span="6">
              <el-form-item prop="type" label="状态" size="small">
                <el-select placeholder="请选择" size="small" v-model="page.state" style="width: 100%">
                  <el-option v-for="item in this.status" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item>
                <el-button type="info" plain size="small" @click="btndele">清除</el-button>
                <el-button type="primary" size="small" @click="search">搜索</el-button>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-row type="flex" justify="end">
                <el-button type="success" size="small" icon="el-icon-edit"  @click="showDialog = true">新增用户</el-button>
              </el-row>
            </el-col>
          </el-row>
        </el-form>
        <!-- alert警告 -->
        <el-alert type="info" show-icon :closable="false" title="before" class="alert">
          <template slot="title">共{{ counts }}条记录</template>
        </el-alert>
        <!-- table菜单页 -->
        <el-table :data="list" v-loading="loading">
          <el-table-column label="序号" align="center" type="index" :index="(index) =>tableIndex(index, page.page, page.pagesize)"/>
          <el-table-column label="企业编号" align="center" prop="number" show-overflow-tooltip />
          <el-table-column label="企业简称" align="center" prop="shortName" show-overflow-tooltip />
          <el-table-column label="标签" align="center" prop="tags" show-overflow-tooltip />
          <el-table-column label="创建者" align="center" prop="creatorID"  show-overflow-tooltip/>
          <el-table-column label="创建日期" align="center" prop="addDate" :formatter="formatTime" show-overflow-tooltip />
          <el-table-column label="备注" align="center" prop="remarks" show-overflow-tooltip />
          <el-table-column label="状态" align="center" prop="state" :formatter="formaStatus" />
          <el-table-column label="操作" align="center">
            <template slot-scope="{ row }">
              <el-tooltip placement="top" content="编辑" effect="light">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  circle
                  plain
                  @click="btnrevise(row.id)"
                />
              </el-tooltip>
              <el-tooltip placement="top" :content="row.content" effect="light">
                <el-button
                  :type="row.type"
                  :icon="row.icon"
                  circle
                  plain
                  @click="btnchange(row.id, row.state)"
                />
              </el-tooltip>
              <el-tooltip placement="top" content="删除" effect="light">
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  circle
                  plain
                  @click="DeleCompany(row.id)"
                />
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="end">
          <el-pagination
            background
            layout="prev, pager, next,sizes,jumper"
            :total="counts"
            :page-sizes="[10, 20]"
            :current-page.sync="page.page"
            :page-size="page.pagesize"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
    </div>
    <companys-add :show-dialog.sync="showDialog" :id="id" ref="child" />
  </div>
</template>

<script>
import CompanysAdd from '@/module-hmmm/components/companys-addzs.vue'
import { CompanyList, DeleCompany, GetCompanyDetail, CompanyState } from '@/api/hmmm/companyszs'
import { status } from '@/api/hmmm/constants.js'
import cityData from '@/api/hmmm/citys.json'
import moment from 'moment'
export default {
  name: 'CompanysIndex',
  components: { CompanysAdd },
  data () {
    return {
      showDialog: false,
      list: [],
      page: {
        page: 1,
        pagesize: 10,
        city: ''
      },
      counts: null,
      cityList: [], // 城市
      areaList: [], // 地区
      cityIndex: 0,
      id: null,
      status
    }
  },
  created () {
    this.CompanyList()
    cityData.data.map((v, i) => {
      this.cityList.push({ label: v.city, value: i })
    })
  },
  methods: {
    // 城市信息
    cityChange (key) {
      this.cityList.map((item) => {
        if (item.label === key) {
          this.cityIndex = item.value
          this.page.city = cityData.data[this.cityIndex].area[0]
        }
      })
      this.areaList = []
      cityData.data[this.cityIndex].area.map((v, i) => {
        this.areaList.push({ label: v, value: i })
      })
    },
    // 数据渲染
    async CompanyList () {
      this.loading = true
      const { data } = await CompanyList(this.page)
      this.list = data.items
      this.counts = data.counts
      this.loading = false
      // 判断按钮显示
      this.list.forEach(item => {
        if (item.state === 0) {
          item.type = 'warning'
          item.icon = 'el-icon-close'
          item.content = '禁用'
        } else {
          item.type = 'success'
          item.icon = 'el-icon-check'
          item.content = '启用'
        }
      })
    },
    // 每页显示数据的条数
    handleSizeChange (val) {
      this.page.pagesize = val
      this.CompanyList()
    },
    // 跳转到某一页
    handleCurrentChange (val) {
      this.page.page = val
      this.CompanyList()
    },
    // 删除按钮
    DeleCompany (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          DeleCompany(id)
          this.CompanyList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 显示状态是启用还是禁用
    formaStatus (row, column, cellValue, index) {
      const obj = status.find(item => item.value === cellValue)
      return obj ? obj.label : '未知'
    },
    // 发送修改状态（启用还是禁用）
    async btnchange (id, state) {
      await this.$confirm(`已${state === 0 ? '禁用' : '启用'}，是否重新${state === 1 ? '禁用' : '启用'}?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      const data = this.list.find(item => item.id === id)
      if (data.state === 1) {
        data.state = 0
      } else {
        data.state = 1
      }
      await CompanyState(id, data.state)
      await this.CompanyList()
    },
    // 点击编辑显示编辑弹层
    async btnrevise (id) {
      this.showDialog = true
      const { data } = await GetCompanyDetail(id)
      this.$refs.child.formData = data
    },
    // 清除按钮
    btndele () {
      this.page = {
        page: 1,
        pagesize: 10,
        city: ''
      }
      this.CompanyList()
    },
    // 时间效果处理
    formatTime (row, column, cellValue, index) {
      return moment(cellValue).format('YYYY/MM/DD hh:mm:ss')
    },
    // 搜索按钮
    async search () {
      this.CompanyList()
    },
    tableIndex (index, currentPage, pageSize) {
      return (
        (currentPage - 1) * pageSize + index + 1
      )
    }
  }
}
</script>

<style rel="stylesheet/scss" scoped lang="scss">
.alert {
  margin-bottom: 20px;
}
</style>
